package com.thread.demo11;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 线程join
 * @author wdd  
 * @date 2019年10月31日
 */
public class JoinDemo1 {
	
	private static int a = 0;
	
	private static void add(){
		int temp = a;
		try {
//			Thread.sleep((int)Math.random()*50);
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		a = temp+1;
	}
	
	private static void test1() throws InterruptedException{
		long d1 = new Date().getTime();
		List<Thread> list = new ArrayList<>();
		for(int i=0;i<100;i++){
			Thread t = new Thread(()->{
				add();
				try {
					Thread.sleep(1000);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+"==");
			});
			list.add(t);
			t.start();
		}
		for(Thread t:list){
			t.join();
		}
		System.out.println("耗时："+(new Date().getTime()-d1));
		
		System.out.println("a:"+a);
		Thread.sleep(1000);
		System.out.println("a:"+a);
	}
	
	private static void test2() throws InterruptedException{
		
	}
	public static void main(String[] args) throws InterruptedException {
		test1();
	}

	
	static class MyThread implements Runnable {
		private Thread thread;
		
		public MyThread(Thread thread) {
			super();
			this.thread = thread;
		}

		public void run() {
			
			try {
				thread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
